import { createApp } from 'vue'
import App from './App.vue'
import { routes } from './router'
// 引入配置文件
import './public-path.js'
import { createRouter, createWebHistory } from 'vue-router'

let router = null
let instance: any = null

function render({ container }: any = {}) {
  router = createRouter({
    routes,
    history: createWebHistory((window as any).__POWERED_BY_QIANKUN__ ? '/child1' : '/'),
  })

  instance = createApp(App)
  instance.use(router).mount(container ? container.querySelector('#app') : '#app')
}

if (!(window as any).__POWERED_BY_QIANKUN__) {
  render()
}

// 生命周期的钩子函数
// 导出第一次进入当前子应用的钩子函数
export async function bootstrap() {
}

// 导出每次创建挂载时的钩子函数
export async function mount(props: any) {
  render(props)
}

// 导出每次销毁时的钩子函数
export async function unmount(props: any) {
  instance.unmount()
  instance._container.innerHTML = ''
  instance = null
  router = null
}
